package com.csoa.community.utils;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author wsj
 */
public class XmlDomParser {
	DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();

	/**
	 * 解析文章raw_content
	 * 
	 * @return
	 */
	public List<String> Parser(String xmlrawContent) {
		if (StringUtils.isEmpty(xmlrawContent))
			return null;
		List<String> lists = new ArrayList<String>();
		try {
			DocumentBuilder dombuilder = domfac.newDocumentBuilder();
			byte[] bytes = xmlrawContent.getBytes();
			ByteArrayInputStream is = new ByteArrayInputStream(bytes);
			Document doc = dombuilder.parse(is);
			Element root = (Element) doc.getElementsByTagName("raw_content").item(0);
			NodeList articles = root.getChildNodes();
			if (articles != null && articles.getLength() > 0) {
				for (int i = 0; i < articles.getLength(); i++) {
					Node article = articles.item(i);
					if (article.getNodeName().toLowerCase().equals("p")) {
						for (int j = 0; j < article.getChildNodes().getLength(); j++) {
							Node sub = article.getChildNodes().item(j);
							if (sub.getNodeName().toLowerCase().equals("img")) {
								String src = sub.getAttributes().getNamedItem("src").getNodeValue();
								lists.add(src);
							} else if (sub.getNodeType() == Node.TEXT_NODE) {
								lists.add(sub.getNodeValue());
							} else if (sub.getNodeType() == Node.ELEMENT_NODE) {
								lists.add(sub.getFirstChild().getNodeValue());
							}
						}
					} else if (article.getNodeName().toLowerCase().equals("img")) {
						String src = article.getAttributes().getNamedItem("src").getNodeValue();
						if (!StringUtils.isEmpty(src)) {
							lists.add(src);
						}
					} else if (article.getNodeType() == Node.TEXT_NODE) {
						if (!article.getNodeValue().trim().equals("")) {
							lists.add(article.getNodeValue());
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lists;
	}

	public class UpgradeInfo {
		public String vername;
		public int vercode;
		public String fname;
		public String url;
		public boolean forceUpgrade;
		public String tip;
		public String title;
		public int totalSize;
	}

	public UpgradeInfo parseUpgradeInfo(String str) {
		if (str == null || str.trim().equals(""))
			return null;
		UpgradeInfo ret = new UpgradeInfo();
		try {
			DocumentBuilder dombuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			byte[] bytes = str.getBytes();
			ByteArrayInputStream is = new ByteArrayInputStream(bytes);
			Document doc = dombuilder.parse(is);
			Element e = doc.getDocumentElement();
			if (e.getElementsByTagName("vname").item(0) != null) {
				if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO) {
					ret.vername = e.getElementsByTagName("vname").item(0).getTextContent();
				} else {
					ret.vername = e.getElementsByTagName("vname").item(0).getFirstChild().getNodeValue();
				}
			}
			if (e.getElementsByTagName("vcode").item(0) != null) {
				if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO) {
					ret.vercode = ConvertUtils.toInt(e.getElementsByTagName("vcode").item(0).getTextContent(), 0);

				} else {
					ret.vercode = ConvertUtils.toInt(e.getElementsByTagName("vcode").item(0).getFirstChild()
							.getNodeValue(), 0);
				}
			}
			if (e.getElementsByTagName("size").item(0) != null) {
				if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO) {
					ret.totalSize = ConvertUtils.toInt(e.getElementsByTagName("size").item(0).getTextContent(), -1);
				} else {
					ret.totalSize = ConvertUtils.toInt(e.getElementsByTagName("size").item(0).getFirstChild()
							.getNodeValue(), -1);
				}
			}
			if (e.getElementsByTagName("url").item(0) != null) {
				if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO) {
					ret.url = e.getElementsByTagName("url").item(0).getTextContent();

				} else {

					ret.url = e.getElementsByTagName("url").item(0).getFirstChild().getNodeValue();
				}
			}
			if (e.getElementsByTagName("fname").item(0) != null) {
				if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO) {
					ret.fname = e.getElementsByTagName("fname").item(0).getTextContent();

				} else {

					ret.fname = e.getElementsByTagName("fname").item(0).getFirstChild().getNodeValue();
				}
			}
			if (e.getElementsByTagName("tip").item(0) != null) {
				if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO) {
					ret.tip = e.getElementsByTagName("tip").item(0).getTextContent();

				} else {

					ret.tip = e.getElementsByTagName("tip").item(0).getFirstChild().getNodeValue();
				}
			}
			if (e.getElementsByTagName("title").item(0) != null) {
				if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO) {
					ret.title = e.getElementsByTagName("title").item(0).getTextContent();

				} else {

					ret.title = e.getElementsByTagName("title").item(0).getFirstChild().getNodeValue();
				}
			}
			if (e.getElementsByTagName("force").item(0) != null) {
				if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO) {
					ret.forceUpgrade = e.getElementsByTagName("force").item(0).getTextContent().trim().equals("1") ? Boolean.TRUE
							: Boolean.FALSE;

				} else {

					ret.forceUpgrade = e.getElementsByTagName("force").item(0).getFirstChild().getNodeValue().trim()
							.equals("1") ? Boolean.TRUE : Boolean.FALSE;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
}